package com.example.wz.lovingpets.utils;

import android.content.Context;
import android.net.Uri;
import android.text.TextPaint;
import android.text.style.ClickableSpan;
import android.view.View;


import com.example.wz.lovingpets.R;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class StringUtils {
    static char[] str = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
            'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
            'X', 'Y', 'Z','a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
            'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
            'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };

    public static String getRandomNum(int number){
        int  maxNum = 62;
        int i;
        int count = 0;
        StringBuilder pwd = new StringBuilder("");
        Random r = new Random();
        while(count < number){
            i = Math.abs(r.nextInt(maxNum));
            if (i >= 0 && i < str.length) {
                pwd.append(str[i]);
                count ++;
            }
        }
        return pwd.toString();
    }

    public static boolean isEmpty(CharSequence str) {
        return str == null || str.length() == 0;
    }

    //有空串则返回true
    public static boolean checkStrings(String... s){
        for(String string :s){
            return isEmpty(string);
        }
        return false;
    }

    public static boolean checkInt(Integer i) {
        return i == null || i == 0;
    }

    public static boolean checkInts(Integer... s){
        for(Integer integer :s){
            return checkInt(integer);
        }
        return false;
    }
    public static List<String> getImgs(String content){
        List<String> list = new ArrayList<>();
        String[] a = content.split("!\\[img\\]\\(");
        for(int i = 1; i<a.length ;i++){
            list.add(a[i].split("\\)")[0]);
        }
        return list;
    }

    public static boolean isMobileNO(String mobileNums) {
        /**
         * 判断字符串是否符合手机号码格式
         * 移动号段: 134,135,136,137,138,139,147,150,151,152,157,158,159,170,178,182,183,184,187,188,199
         * 联通号段: 130,131,132,145,155,156,170,171,175,176,185,186
         * 电信号段: 133,149,153,170,173,177,180,181,189
         * @param str
         * @return 待检测的字符串
         */
        String telRegex = "^((13[0-9])|(14[5,7,9])|(15[^4])|(18[0-9])|(199)|(17[0,1,3,5,6,7,8]))\\d{8}$";// "[1]"代表下一位为数字可以是几，"[0-9]"代表可以为0-9中的一个，"[5,7,9]"表示可以是5,7,9中的任意一位,[^4]表示除4以外的任何一个,\\d{9}"代表后面是可以是0～9的数字，有9位。
            return mobileNums.matches(telRegex) && isEmpty(mobileNums);
    }

    public static String getMoney(float cost) {
        DecimalFormat decimalFormat = new DecimalFormat("0.00");
        return "￥" + decimalFormat.format(cost);
    }

//    用于分割地址的方法
    public static String[] splitWithBlank(String s){
        String[] temp = null;
        temp = s.split(" ");
        return temp;
    }

    public static String formatUrl(Uri uri){
        String res = uri.toString().substring(36);
        res = res.replace("%2F","/");
        return res;
    }

    public static class PrimaryClickableSpan extends ClickableSpan {

        private Context context;

        public PrimaryClickableSpan(Context context) {
            this.context = context;
        }

        @Override
        public void onClick(View widget) {
            // do nothing
        }

        @Override
        public void updateDrawState(TextPaint ds) {
            ds.setColor(context.getResources().getColor(R.color.colorPrimary_dog));
            ds.setUnderlineText(false);
        }
    }
}
